package com.tbit.uqbike.client.controller;

import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.Adoption;
import com.tbit.uqbike.client.service.LeaseService;
import com.tbit.uqbike.client.util.OutputUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 快码租赁
 *
 * @AUTHOR: wei.xing
 * @DATE: 2023-04-19 13:36
 **/
@Slf4j
@RestController
@RequestMapping("/lease")
public class LeaseController {

    @Autowired
    private LeaseService leaseService;


    /**
     * 查询车牌号前缀
     *
     * @param accountId
     * @param request
     * @param response
     * @param session
     */
    @GetMapping("/getLicensePrefix")
    public void getLicensePrefix(Integer accountId, HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            String licensePrefix = leaseService.getLicensePrefix(accountId);
            map.put(ResponseCode.DATA, licensePrefix);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);
    }

    /**
     * 通过车牌号查询MachineNO
     *
     * @param accountId
     * @param license
     * @param request
     * @param response
     * @param session
     */
    @GetMapping("/getMachineNO")
    public void getByMachineNO(Integer accountId, String license, HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            String machineNO = leaseService.getMachineNO(accountId, license);
            map.put(ResponseCode.DATA, machineNO);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);
    }

    /**
     * 领养上报
     *
     * @param adoption
     * @param request
     * @param response
     * @param session
     */
    @PostMapping("/addAdoption")
    public void addAdoption(Adoption adoption, HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            leaseService.addAdoption(adoption);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);
    }


}
